package com.bluetrum.abmate;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bluetrum.abmate.adapter.GridSpaceItemDecoration;
import com.bluetrum.abmate.adapter.BtDeviceFunctionsAdapter;
import com.bluetrum.abmate.adapter.DeviceFunctionUnit;
import com.bluetrum.abmate.databinding.FragmentHomeBinding;
import com.bluetrum.abmate.viewmodels.SharedViewModel;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class HomeFragment extends Fragment implements BtDeviceFunctionsAdapter.OnItemClickListener {

    private String strConnectDevice;

    private FragmentHomeBinding binding;

    private SharedViewModel mViewModel;

    private BtDeviceFunctionsAdapter mAdapter;
    private StaggeredGridLayoutManager mLayoutManager;
    private GridSpaceItemDecoration mItemDecoration;

    private static final int RECYCLER_VIEW_SPAN_COUNT = 3;

    private static final int ROW_SPACING = 4;
    private static final int COLUMN_SPACING = 4;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(this).get(SharedViewModel.class);
        binding = FragmentHomeBinding.inflate(inflater, container, false);

        // Load resources
        strConnectDevice = getResources().getString(R.string.home_function_connect_device);

        setupRecyclerView();

        // 当前正在使用的设备
        mViewModel.getActiveDevice().observe(getViewLifecycleOwner(), btDevice -> mAdapter.setBtDevice(btDevice));

        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    private void setupRecyclerView() {
        int spanCount = RECYCLER_VIEW_SPAN_COUNT;
        mLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);
        binding.deviceFunctionRecyclerView.setLayoutManager(mLayoutManager);

        mItemDecoration = new GridSpaceItemDecoration(spanCount, ROW_SPACING, COLUMN_SPACING);
        binding.deviceFunctionRecyclerView.addItemDecoration(mItemDecoration);

        mAdapter = new BtDeviceFunctionsAdapter(mViewModel.getActiveDevice().getValue(), spanCount);
        mAdapter.setOnItemClickListener(this);
        binding.deviceFunctionRecyclerView.setAdapter(mAdapter);
    }

    // 功能选择
    @Override
    public void OnItemClick(DeviceFunctionUnit functionUnit) {
        if (mViewModel.getActiveDevice().getValue() != null) {
            Intent intent = new Intent(requireContext(), functionUnit.getFuncClass());
            startActivity(intent);
        } else {
            Toast.makeText(requireActivity(), strConnectDevice, Toast.LENGTH_SHORT).show();
        }
    }
}